package edu.hitsz.scoreDAO;

import edu.hitsz.application.Main;
import java.io.*;
import java.util.LinkedList;


public class ScoreDaoFile implements ScoreDao {
    private LinkedList<UserInfo> allUsers;
    //存储路径
    private String pathUserInfos;

    public ScoreDaoFile() throws IOException {
        this.setPath();
        this.allUsers=this.getAllUsers();
    }

    public void setPath(){
        if(Main.gameMode==Main.EASY_MODE){
            pathUserInfos="easy_userInfo.txt";
        } else if (Main.gameMode==Main.NORMAL_MODE) {
            pathUserInfos="normal_userInfo.txt";
        }else{
            pathUserInfos="hard_userInfo.txt";
        }
    }


    /**
     * 读取文件，获取用户数据列表
     * @return
     * @throws IOException
     */

    @Override
    public LinkedList<UserInfo> getAllUsers() throws IOException {

        LinkedList<UserInfo> userInfos=new LinkedList<>();

        File file = new File(pathUserInfos);
        //文件不存在则创建文件
        if(!file.exists()){
            file.createNewFile();
        }
        else {
            FileReader reader = new FileReader(file);
            BufferedReader bfReader = new BufferedReader(reader);
            //逐行读取文件
            String info=bfReader.readLine();
            while (info!=null){
                UserInfo userInfo= parseInfo(info);
                userInfos.add(userInfo);
                info=bfReader.readLine();
            }
            bfReader.close();
            reader.close();
        }
        return userInfos;
    }

    //正则表达式，解析读入行
    public UserInfo parseInfo(String info){
        String[] tokens=info.split("\\|");
        int rank=Integer.parseInt(tokens[0]);
        String name= tokens[1];
        int score = Integer.parseInt(tokens[2]);
        String time= tokens[3];
        return new UserInfo(rank,name,score,time);
    }

    /**
     * 添加新用户信息
     * @param userName
     * @param score
     * @throws IOException
     */
    @Override
    public void doAdd(String userName ,int score) throws IOException {
        UserInfo newUser=new UserInfo(userName,score);
        allUsers=rankScore(allUsers,newUser);

        File file= new File(pathUserInfos);
        FileWriter writer= new FileWriter(file);
        BufferedWriter bfWriter=new BufferedWriter(writer);

        for (UserInfo user:allUsers){
            String s=user.getRank()+"|"+user.getUserName()+"|"+user.getScore()+"|"+user.getTime()+"\n";
            writer.append(s);
        }

        bfWriter.close();
        writer.close();
    }

    //重新对分数排序
    public LinkedList<UserInfo> rankScore(LinkedList<UserInfo> allUsers,UserInfo newUser){
        int addPosition=0;

        for (UserInfo user:allUsers){
            if(user.getScore()>newUser.getScore()){
                addPosition+=1;
            }else {
                break;
            }
        }

        newUser.setRank(addPosition+1);
        allUsers.add(addPosition,newUser);

        int newRank=1;
        for (UserInfo user:allUsers){
            user.setRank(newRank);
            newRank+=1;
        }
        return allUsers;
    }

    @Override
    public void doDelete(int rank) throws IOException {
        allUsers.remove(rank-1);
        //删除后修改排名
        int i=1;
        for(UserInfo user:allUsers) {
            user.setRank(i);
            i += 1;
        }

        File file= new File(pathUserInfos);
        FileWriter writer= new FileWriter(file);
        BufferedWriter bfWriter=new BufferedWriter(writer);

        for (UserInfo user:allUsers){
            String s=user.getRank()+"|"+user.getUserName()+"|"+user.getScore()+"|"+user.getTime()+"\n";
            writer.append(s);
        }

        bfWriter.close();
        writer.close();
    }
}
